/**
 * Created by ancheel on 2017/3/22.
 */
'use strict';

const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout))


export default {
  namespace: 'example',

  state: 10
  ,

  effects: {
    *start(action, effects) {

      const {put, call, fork, select, takeEvery} = effects

      let interval = yield select(state => state.example)

      function *demo() {
        while (1) {
          yield call(delay, 500)
          yield put({type: 'setInterval', payload: ++interval})
        }
      }

      const d = yield fork(demo)

      yield takeEvery('example/pause', function *() {
        yield d.cancel()
      })

      yield takeEvery('example/stop', ( function *() {
          yield select(state => state.example)
          yield d.cancel()

        })
      )


    },
  },

  reducers: {
    setInterval(state, {payload}) {
      return payload
    },
    clear(state, {payload}){

      return payload
    }
  }
}
